{% extends "base.html" %}
{% import "_form_helpers.html" as forms %}

{% block title %}添加自定义数据集 - {{ super() }}{% endblock %}

{% block content %}
<div class="max-w-2xl mx-auto py-8">
    <div class="prose max-w-none mb-6">
        <h1>添加自定义数据集</h1>
        <p>请填写以下信息来创建新的自定义数据集。确保信息准确无误。</p>
    </div>

    <div class="card bg-base-200 shadow-xl">
        <form method="POST" novalidate class="card-body space-y-4" enctype="multipart/form-data">
            {{ form.hidden_tag() }}

            {{ forms.render_field(form.name, placeholder='例如：医学问答数据集', required=True) }}
            
            {{ forms.render_field(form.description, placeholder='详细描述数据集的内容、目的、结构等信息。', html_attrs={'rows': '4'}) }}
            
            <div class="form-control w-full">
                <label class="label" for="{{ form.format.id }}">
                    <span class="label-text">{{ form.format.label.text }}<span class="text-error">*</span></span>
                </label>
                {{ form.format(class="select select-bordered w-full") }}
                {% if form.format.errors %}
                    <label class="label">
                        <span class="label-text-alt text-error">
                            {% for error in form.format.errors %}{{ error }}<br>{% endfor %}
                        </span>
                    </label>
                {% endif %}
            </div>

            <div class="form-control">
                <div class="collapse collapse-arrow border border-base-300 bg-base-100 rounded-box">
                    <input type="checkbox" /> 
                    <div class="collapse-title font-medium">
                        数据格式说明
                    </div>
                    <div class="collapse-content"> 
                        <div id="qa-format-info" class="mb-4">
                            <h3 class="font-bold mb-2">问答题格式 (QA) - JSONL文件</h3>
                            <div class="mockup-code max-h-60 overflow-y-auto">
                                <pre data-prefix="1"><code>{"system": "xx", "query": "xx", "response": "xx"}</code></pre>
                                <pre data-prefix="2"><code>{"query": "xx", "response": "xx"}</code></pre>
                                <pre data-prefix="3"><code>{"query": "xx", "response": "xx"}</code></pre>
                            </div>
                            <p class="text-sm mt-2">
                                <span class="font-bold">system</span>: 系统提示（可选字段）<br>
                                <span class="font-bold">query</span>: 问题（必须字段）<br>
                                <span class="font-bold">response</span>: 正确回答（必须字段）
                            </p>
                        </div>
                        
                        <div id="mcq-format-info" class="mb-4 hidden">
                            <h3 class="font-bold mb-2">选择题格式 (MCQ) - CSV文件</h3>
                            <div class="mockup-code max-h-60 overflow-y-auto">
                                <pre data-prefix="1"><code>id,question,A,B,C,D,answer</code></pre>
                                <pre data-prefix="2"><code>1,问题x,4种,22种,20种,19种,C</code></pre>
                                <pre data-prefix="3"><code>2,问题x,尿素,尿酸,丙酮酸,二氧化碳,C</code></pre>
                            </div>
                            <p class="text-sm mt-2">
                                <span class="font-bold">id</span>: 序号（可选字段）<br>
                                <span class="font-bold">question</span>: 问题（必须字段）<br>
                                <span class="font-bold">A,B,C,D等</span>: 选项（最多支持10个选项，A-J）<br>
                                <span class="font-bold">answer</span>: 正确选项（如A,B,C,D）
                            </p>
                        </div>
                        
                        <div id="fill-format-info" class="mb-4 hidden">
                            <h3 class="font-bold mb-2">自定义格式 (CUSTOM) - JSONL文件</h3>
                            <div class="mockup-code max-h-60 overflow-y-auto">
                                <pre data-prefix="1"><code>{"history": [{"user": "xx", "assistant": "xx"}], "question": "xx","answer": "xxx"}</code></pre>
                                <pre data-prefix="2"><code>{"question": "xx", "answer": "xx"}</code></pre>
                            </div>
                            <p class="text-sm mt-2">
                                <span class="font-bold">history</span>: 历史对话信息，首轮不用填，有的话使用list格式<br>
                                <span class="font-bold">question</span>: 用户问题<br>
                                <span class="font-bold">answer</span>: 正确答案（必须字段）<br>
                            </p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Jinja2模板输入区域 -->
            <div class="form-control w-full jinja2-template-field" style="display: none;">
                <label class="label" for="{{ form.jinja2_template.id }}">
                    <span class="label-text">{{ form.jinja2_template.label.text }}
                </label>
                {{ form.jinja2_template(class="textarea textarea-bordered w-full h-64 font-mono", placeholder='{# 自定义数据集不填写jinja2模版将无法进行效果评估，但是不影响性能评估 #}

{# 获取数据集配置 #}
{% macro get_config() %}
{
    "llm_as_a_judge": false  {# 是否使用LLM作为裁判，默认为false #}
}
{% endmacro %}

{# 自定义提示词生成 #}
{% macro gen_prompt(system_prompt, history, user_prompt) %}
{# 自定义提示词生成逻辑 #}
{% endmacro %}

{# 自定义标准答案获取 #}
{% macro get_gold_answer(input_d) %}
{# 自定义标准答案获取逻辑 #}
{% endmacro %}

{# 自定义预测结果解析 #}
{% macro parse_pred_result(result) %}
{# 自定义预测结果解析逻辑 #}
{% endmacro %}

{# 自定义结果比较 #}
{% macro match(gold, pred) %}
{# 自定义结果比较逻辑 #}
{% endmacro %}

{# 自定义指标计算 #}
{% macro compute_metric(review_res_list) %}
{# 自定义指标计算逻辑 #}
{% endmacro %}') }}
                {% if form.jinja2_template.errors %}
                <label class="label">
                    <span class="label-text-alt text-error">
                        {% for error in form.jinja2_template.errors %}{{ error }}<br>{% endfor %}
                    </span>
                </label>
                {% endif %}
                <label class="label pt-0">
                    <span class="label-text-alt">模板需要包含以下宏：gen_prompt, get_gold_answer, match, parse_pred_result,
                        compute_metric</span>
                </label>
            </div>
            
            <div class="form-control w-full">
                <label class="label">
                    <span class="label-text">{{ form.categories.label.text }}</span>
                </label>
                <div class="grid grid-cols-2 sm:grid-cols-3 gap-2 p-2 border border-base-300 rounded-md max-h-48 overflow-y-auto">
                    {% for subfield in form.categories %}
                    <label class="label cursor-pointer justify-start">
                        {{ subfield(class='checkbox checkbox-sm checkbox-primary') }}
                        <span class="label-text ml-2">{{ subfield.label.text }}</span>
                    </label>
                    {% endfor %}
                </div>
                {% if form.categories.errors %}
                    <label class="label">
                        <span class="label-text-alt text-error">
                            {% for error in form.categories.errors %}{{ error }}<br>{% endfor %}
                        </span>
                    </label>
                {% endif %}
                {% if form.categories.description %}
                    <label class="label pt-0">
                        <span class="label-text-alt">{{ form.categories.description }}</span>
                    </label>
                {% endif %}
            </div>
            
            {{ forms.render_field(form.dataset_file, type='file') }}

            <div class="form-control w-full">
                <label class="label" for="{{ form.visibility.id }}">
                    <span class="label-text">{{ form.visibility.label.text }}<span class="text-error">*</span></span>
                </label>
                {{ form.visibility(class="select select-bordered w-full") }}
                {% if form.visibility.errors %}
                    <label class="label">
                        <span class="label-text-alt text-error">
                            {% for error in form.visibility.errors %}{{ error }}<br>{% endfor %}
                        </span>
                    </label>
                {% endif %}
            </div>

            <div class="card-actions justify-end pt-4">
                <a href="{{ url_for('datasets.datasets_list') }}" class="btn btn-ghost">取消</a>
                {{ forms.render_submit_button(form.submit, class='btn-primary', id='submit-btn') }}
            </div>
        </form>
    </div>
</div>

<!-- 上传进度模态框 -->
<input type="checkbox" id="upload-modal" class="modal-toggle" />
<div class="modal">
    <div class="modal-box">
        <h3 class="font-bold text-lg">
            <i class="fas fa-upload mr-2"></i>正在上传和处理数据集
        </h3>
        <div class="py-4">
            <p class="mb-4" id="upload-status">正在上传文件，请稍候...</p>
            <progress class="progress progress-primary w-full"></progress>
            <p class="text-sm text-gray-500 mt-2">
                <span id="file-info"></span><br>
                正在验证文件格式，请不要关闭页面。
            </p>
        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const formatSelect = document.getElementById('format');
    const jinja2TemplateField = document.querySelector('.jinja2-template-field');
    const qaFormatInfo = document.getElementById('qa-format-info');
    const mcqFormatInfo = document.getElementById('mcq-format-info');
    const fillFormatInfo = document.getElementById('fill-format-info');

    updateFormatInfo(formatSelect.value);

    function updateFormatInfo(format) {
        // 隐藏所有格式说明
        qaFormatInfo.classList.add('hidden');
        mcqFormatInfo.classList.add('hidden');
        fillFormatInfo.classList.add('hidden');

        const selectedFormat = formatSelect.value;
        if (selectedFormat === 'CUSTOM') {
            jinja2TemplateField.style.display = 'block';
        } else {
            jinja2TemplateField.style.display = 'none';
        }

        // 显示对应的格式说明
        if (format === 'QA') {
            qaFormatInfo.classList.remove('hidden');
        } else if (format === 'MCQ') {
            mcqFormatInfo.classList.remove('hidden');
        } else if (format === 'CUSTOM') {
            fillFormatInfo.classList.remove('hidden');
        }
    }
    
    formatSelect.addEventListener('change', function () {
        updateFormatInfo(this.value);
    });
    updateFields(); // 初始化时执行一次
});
</script>
{% endblock %} 